﻿/*
 * Copyright 2010 Marc Cesarine
 * 
 * This file is part of Ssh Commander.

 * Ssh Commander is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * Ssh Commander is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with Ssh Commander.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Xml;
using System.Xml.Schema;

namespace SshCommander
{
    /// <summary>
    /// Logique d'interaction pour ListImport.xaml
    /// </summary>
    public partial class ListImport : UserControl
    {
        private ServersList serversList_v;
        private string filename_v;
        private bool fileChecked;

        public ListImport(ref ServersList serverList)
        {
            InitializeComponent();
            serversList_v = serverList;
            fileChecked = false;
        }

        private void ImportButton_Click(object sender, RoutedEventArgs e)
        {
            if (fileChecked)
            {
                bool error = false;
                serversList_v.Clear();

                Assembly MyAssembly;
                Stream XSDImageStream;

                MyAssembly = Assembly.GetExecutingAssembly();
                XSDImageStream = MyAssembly.GetManifestResourceStream("SshCommander.xsd.Input.xsd");

                //XmlReader SchemaReader = XmlReader.Create("xsd/PasswordManagement.xsd");
                XmlReader SchemaReader = XmlReader.Create(XSDImageStream);

                XmlSchemaSet sc = new XmlSchemaSet();
                XmlSchema sch = XmlSchema.Read(SchemaReader, null);

                // Add the schema to the collection.
                sc.Add(sch);

                // Set the validation settings.
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.ValidationType = ValidationType.Schema;
                settings.Schemas = sc;

                // Create the XmlReader object.
                XmlReader reader = XmlReader.Create(filename_v, settings);

                try
                {
                    while (reader.Read())
                    {
                        switch (reader.NodeType)
                        {
                            case XmlNodeType.Element: // The node is an element.
                                switch (reader.Name)
                                {
                                    case "tunnel": //The node is the tunnel element
                                        Tunnel tunnel = new Tunnel();
                                        while (reader.MoveToNextAttribute())
                                        {
                                            switch (reader.Name)
                                            {
                                                case "id":
                                                    tunnel.id = reader.Value;
                                                    break;
                                                case "hostname":
                                                    tunnel.hostname = reader.Value;
                                                    break;
                                                case "username":
                                                    tunnel.username = reader.Value;
                                                    break;
                                                case "password":
                                                    tunnel.password = reader.Value;
                                                    break;
                                            }
                                        }
                                        serversList_v.addTunnel(tunnel);
                                        break;
                                    case "server": //The node is the server element
                                        Server server = new Server();
                                        serversList_v.addServer(server);

                                        while (reader.MoveToNextAttribute())
                                        {
                                            switch (reader.Name)
                                            {
                                                case "id":
                                                    server.id = reader.Value;
                                                    break;
                                                case "name":
                                                    server.name = reader.Value;
                                                    break;
                                                case "hostname":
                                                    server.hostname = reader.Value;
                                                    break;
                                                case "superusername":
                                                    server.superusername = reader.Value;
                                                    break;
                                                case "superuserpass":
                                                    server.superuserpass = reader.Value;
                                                    break;
                                                case "tunnel_id":
                                                    string tunnel_id = reader.Value;
                                                    serversList_v.linkServerTunnel(server.id, tunnel_id);
                                                    break;
                                            }
                                        }
                                        break;
                                    case "account": //The node is the server element
                                        Account account = new Account();
                                        while (reader.MoveToNextAttribute())
                                        {
                                            switch (reader.Name)
                                            {
                                                case "id":
                                                    account.id = reader.Value;
                                                    break;
                                                case "username":
                                                    account.login = reader.Value;
                                                    break;
                                                case "password":
                                                    account.password = reader.Value;
                                                    break;
                                            }
                                        }
                                        serversList_v.addAccount(account);
                                        break;
                                    case "link": //The node is the server element
                                        string serverid = "";
                                        string accountid = "";
                                        while (reader.MoveToNextAttribute())
                                        {
                                            switch (reader.Name)
                                            {
                                                case "server":
                                                    serverid = reader.Value;
                                                    break;
                                                case "account":
                                                    accountid = reader.Value;
                                                    break;
                                            }
                                        }
                                        serversList_v.linkServerAccount(serverid, accountid);
                                        break;
                                }
                                break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    error = true;
                }
                finally
                {
                    reader.Close();
                }

                if (!error)
                {
                    StatusLabel.Content = "Import successfully terminated";
                }
            }
        }

        private void FileButton_Click(object sender, RoutedEventArgs e)
        {
            //Create OpenFile window object
            Microsoft.Win32.OpenFileDialog OpenFileDialog = new Microsoft.Win32.OpenFileDialog();

            //Configure OpenFile window filters
            OpenFileDialog.FileName = "Configuration file";
            OpenFileDialog.DefaultExt = ".xml";
            OpenFileDialog.Filter = "XML documents (.xml)|*.xml";

            //Display OpenFile window
            Nullable<bool> DialogResult = OpenFileDialog.ShowDialog();

            //If OK, we continue
            if (DialogResult == true)
            {
                //Get Filename from OpenFile window
                filename_v = OpenFileDialog.FileName;
                FileTextBox.Text = filename_v;
                fileChecked = true;
            }
        }
    }
}
